{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 使用SVM对手写数字的识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import matplotlib as mpl\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "from sklearn import datasets, svm, metrics"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "## 设置属性防止中文乱码\n",
    "mpl.rcParams['font.sans-serif'] = [u'SimHei']\n",
    "mpl.rcParams['axes.unicode_minus'] = False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "## 加载数字图片数据\n",
    "digits = datasets.load_digits()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "## 获取样本数量，并将图片数据格式化\n",
    "n_samples = len(digits.images)\n",
    "data = digits.images.reshape((n_samples, -1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,\n",
       "  decision_function_shape=None, degree=3, gamma=0.001, kernel='rbf',\n",
       "  max_iter=-1, probability=False, random_state=None, shrinking=True,\n",
       "  tol=0.001, verbose=False)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 模型构建\n",
    "classifier = svm.SVC(gamma=0.001)\n",
    "## 使用二分之一的数据进行模型训练\n",
    "classifier.fit(data[:int(n_samples / 2)], digits.target[:int(n_samples / 2)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "分类器SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,\n",
      "  decision_function_shape=None, degree=3, gamma=0.001, kernel='rbf',\n",
      "  max_iter=-1, probability=False, random_state=None, shrinking=True,\n",
      "  tol=0.001, verbose=False)的分类效果:\n",
      "             precision    recall  f1-score   support\n",
      "\n",
      "          0       1.00      0.99      0.99        88\n",
      "          1       0.99      0.97      0.98        91\n",
      "          2       0.99      0.99      0.99        86\n",
      "          3       0.98      0.87      0.92        91\n",
      "          4       0.99      0.96      0.97        92\n",
      "          5       0.95      0.97      0.96        91\n",
      "          6       0.99      0.99      0.99        91\n",
      "          7       0.96      0.99      0.97        89\n",
      "          8       0.94      1.00      0.97        88\n",
      "          9       0.93      0.98      0.95        92\n",
      "\n",
      "avg / total       0.97      0.97      0.97       899\n",
      "\n",
      "\n",
      "混淆矩阵为:\n",
      "[[87  0  0  0  1  0  0  0  0  0]\n",
      " [ 0 88  1  0  0  0  0  0  1  1]\n",
      " [ 0  0 85  1  0  0  0  0  0  0]\n",
      " [ 0  0  0 79  0  3  0  4  5  0]\n",
      " [ 0  0  0  0 88  0  0  0  0  4]\n",
      " [ 0  0  0  0  0 88  1  0  0  2]\n",
      " [ 0  1  0  0  0  0 90  0  0  0]\n",
      " [ 0  0  0  0  0  1  0 88  0  0]\n",
      " [ 0  0  0  0  0  0  0  0 88  0]\n",
      " [ 0  0  0  1  0  1  0  0  0 90]]\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Administrator\\Anaconda3\\lib\\site-packages\\ipykernel\\__main__.py:2: VisibleDeprecationWarning: using a non-integer number instead of an integer will result in an error in the future\n",
      "  from ipykernel import kernelapp as app\n",
      "C:\\Users\\Administrator\\Anaconda3\\lib\\site-packages\\ipykernel\\__main__.py:3: VisibleDeprecationWarning: using a non-integer number instead of an integer will result in an error in the future\n",
      "  app.launch_new_instance()\n"
     ]
    }
   ],
   "source": [
    "## 测试数据部分实际值和预测值获取\n",
    "expected = digits.target[n_samples / 2:]\n",
    "predicted = classifier.predict(data[n_samples / 2:])\n",
    "\n",
    "## 计算准确率\n",
    "print(\"分类器%s的分类效果:\\n%s\\n\"\n",
    "      % (classifier, metrics.classification_report(expected, predicted)))\n",
    "print(\"混淆矩阵为:\\n%s\" % metrics.confusion_matrix(expected, predicted))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Administrator\\Anaconda3\\lib\\site-packages\\ipykernel\\__main__.py:4: VisibleDeprecationWarning: using a non-integer number instead of an integer will result in an error in the future\n",
      "C:\\Users\\Administrator\\Anaconda3\\lib\\site-packages\\ipykernel\\__main__.py:11: VisibleDeprecationWarning: using a non-integer number instead of an integer will result in an error in the future\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAA0EAAAF2CAYAAACoHZPlAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3X9s1fW9P/BXS1tkaCkMvIhjVEG9kgutRucmblS9cxq8\nW4lu8cbdUbe47GaaL2bJ/rX+v2SYbGYz28Rkmy6aDH9MuNsyCjrxx+4sEURRbtvBBCk/+oMChf74\n/kF6pNBCYW/W0vfjkZjY8unzvN+nr37OeZ7Tc1pUX1/fHwAAAJkoHu0FAAAA/DMpQQAAQFaUIAAA\nICtKEAAAkBUlCAAAyIoSBAAAZGVclKCenp7o6+s76XP9/f3R0tJy2q9fv3599Pb2Fj7u7++P7u7u\nwsd9fX1x5MiR0+b09fXFhx9+OOS/vfrqq9He3n7ajDNxtus8U6faF4OZxY8v3yyOPvP48eWbx9Fl\nFj++fLM4uszix5ef+yyWjPYCzlRHR0f86le/iokTJ8aRI0eirKws5syZEy0tLdHe3h4TJkyIiy66\nKHp7e+Puu++O9evXR2lpacybNy/++te/RmlpaRw9ejSqqqri+uuvj6KionjjjTfi85//fOEympub\n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QitMmTJ8fChQsL/534Irbu\n7u7Yt29fXHLJJYM+f9FFF8XSpUvjzTffjKKiopg3b160t7fHJz/5yWH3VlFREdOnT48PPvggXnnl\nlXj77bcL//brX/86Ojs7z3qdQ7n++uvjjjvuiK6urti5c2fcdNNNp9xXdXV1VFVVRUTE7NmzY8qU\nKbFr167TXs54ZRbN4lhiHkdvHhsaGqKqquqUe8iJWUw3i6c61w23r4hjL06fP3/+afPHO7OYbhYH\nDDVbp5rFG2+8MR588MF4+eWXY+/evSO+nH+G8/5h029/+9uxY8eOiIjYvXt3YaAHBnb79u2xcePG\nWLJkSezbty/Ky8tj1qxZMW3atNi7d++Qg3280tLSmDZt2qCPj/fGG2/EZz7zmUGf2759e6xatarw\ntOJvfvOb6O/vjz179sTu3bujuLg4+vv748iRI/H1r3+98PuRO3bsKLxY7fXXX49ly5YV9lZeXh7r\n1q2LO++886zWOZyrr746iouLo6mpKa6++upT7quxsTH+7d/+rfAIVGdn5ykfJcmNWRzZOodjFtMy\njyNb53DOZB63bt0aBw8ejA0bNkRvb2/09PREd3d33HPPPSO6rPHOLI5snUM51bluqH0N2LJlS+EF\n6XzMLI5snacy1GwNta+9e/fG0aNHY+bMmTFlypSYNWtW7N27d0w9WHTel6CIY432qquuik2bNkV1\ndXWUlpbGtddeGxHH3mt98eLFUVlZGe+9915cfPHFsW7durjrrrsKvxd6Km1tbdHQ0DDo4wGHDh2K\nrq6uk37Hcfbs2fHggw8WPu7v74+XXnopKisrY/bs2TF37txB7zI08NaX77zzTlx11VVx8ODBqKys\nLLxjR0TETTfdFD/+8Y9j0aJFQ675VOs8nZdffjluvfXWwol1uH21tLREZ2dn3HDDDdHY2BgTJkzw\n4vQTmEWzOJaYx3/OPH73u98t/H9TU1M0NjZm/+5wJzKLZzeLw53rhttXRMTBgwdj//792b8hwnDM\n4tmfF4eareH21dnZGS+++GJ861vfip6envjwww9jyZIlI7qcf5bz7tfhBvT09BRumKZOnRo9PT1x\n8ODBKCsri6effjqmT58e27Z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1q3bl2yrFTXVXV1dZKc8Wb58uVJcmpra5PkREQ0\nNjYmy6qrq0uW1d7eniQn5c/aeJPq+7Vy5cokORFpZyjlbWKqn5Pz/XbaM0EAAEBWlCAAACArShAA\nAJAVJQgAAMiKEgQAAGRFCQIAALKiBAEAAFlRggAAgKwoQQAAQFaUIAAAICtKEAAAkBUlCAAAyIoS\nBAAAZEUJAgAAsqIEAQAAWVGCAACArChBAABAVpQgAAAgKyWjvYDzyVe+8pVkWc3NzcmyKioqkuTU\n1dUlyWF4y5cvT5ZVW1ubLKulpSVZ1hNPPJEkp7q6OknOeLNy5crRXsJJUq7pySefTJZVX1+fLIuT\npbrtaWhoSJITkfa8cfPNNyfLevjhh5PkOC+ee6tWrUqWlfJ+VcrzWcr7IuczzwQBAABZUYIAAICs\nKEEAAEBWlCAAACArShAAAJAVJQgAAMiKEgQAAGRFCQIAALKiBAEAAFlRggAAgKwoQQAAQFaUIAAA\nICtKEAAAkBUlCAAAyIoSBAAAZEUJAgAAsqIEAQAAWVGCAACArBTV19f3j/YiAAAA/lk8EwQAAGRF\nCQIAALKiBAEAAFlRggAAgKwoQQAAQFaUIAAAICtKEAAAkBUlCAAAyIoSBAAAZEUJAgAAsqIEAQAA\nWVGCAACArChBAABAVpQgAAAgK0oQAACQFSUIAADIihIEAABkRQkCAACyogQBAABZUYIAAICsKEEA\nAEBW/j9EytkfYkpJqwAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x2ad21290940>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "## 进行图片展示\n",
    "plt.figure(facecolor='gray', figsize=(12,5))\n",
    "## 先画出5个预测失败的\n",
    "images_and_predictions = list(zip(digits.images[n_samples / 2:][expected != predicted], expected[expected != predicted], predicted[expected != predicted]))\n",
    "for index, (image,expection, prediction) in enumerate(images_and_predictions[:5]):\n",
    "    plt.subplot(2, 5, index + 1)\n",
    "    plt.axis('off')\n",
    "    plt.imshow(image, cmap=plt.cm.gray_r, interpolation='nearest')\n",
    "    plt.title(u'预测值/实际值:%i/%i' % (prediction, expection))\n",
    "## 再画出5个预测成功的\n",
    "images_and_predictions = list(zip(digits.images[n_samples / 2:][expected == predicted], expected[expected == predicted], predicted[expected == predicted]))\n",
    "for index, (image,expection, prediction) in enumerate(images_and_predictions[:5]):\n",
    "    plt.subplot(2, 5, index + 6)\n",
    "    plt.axis('off')\n",
    "    plt.imshow(image, cmap=plt.cm.gray_r, interpolation='nearest')\n",
    "    plt.title(u'预测值/实际值:%i/%i' % (prediction, expection))\n",
    "\n",
    "plt.subplots_adjust(.04, .02, .97, .94, .09, .2)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [default]",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
